<?php
namespace app\material\controller\sys_admin;
use app\AdminController;

use app\distribution\model\DividendRoleModel;
use app\mainadmin\model\SettingsModel;
use app\material\model\MaterialCategoryModel;
use app\material\model\MaterialListModel;
use app\material\model\MaterialOrderModel;
use app\shop\model\GoodsModel;
use think\Db;

//use function app\school\controller\sys_admin\makeDir;

/**
 * 高清大图
 */
class Setting extends AdminController
{
	/*------------------------------------------------------ */
	//-- 优先执行
	/*------------------------------------------------------ */
	public function initialize(){
        parent::initialize();
        $this->Model = new MaterialOrderModel();
    }
	/*------------------------------------------------------ */
	//-- 主页
	/*------------------------------------------------------ */
    public function index(){

        $setModel = new SettingsModel();
        $DividendRoleModel = new DividendRoleModel();
        $material_banner = $setModel->where(['name' => 'material_banner'])->value('data');
        $material_auth = $setModel->where(['name' => 'material_auth'])->value('data');
        $this->assign("UsersRole", $DividendRoleModel->getRows()); //分销身份
        $this->assign("setting", ['material_banner' => $material_banner,'material_auth' => $material_auth ? explode(',',$material_auth) : []]);
		return $this->fetch();
	}

    public function save(){
        $set = input('post.setting');
        if($set['material_auth']){
            $set['material_auth'] = implode(',',$set['material_auth']);
        }
        $setModel = new SettingsModel();
        $res = $setModel->editSave($set);
        if ($res == false) return $this->error();
        return $this->success('设置成功.');
    }

    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false)
    {
        $where = [];
        $search['keyword'] = input('keyword', '', 'trim');
        $search['type'] = input('type', '', 'trim');
        if (empty($search['keyword']) == false) $where[] = ['name', 'like', '%' . $search['keyword'] . '%'];
        if (empty($search['type']) == false) $where[] = ['type', '=', $search['type']];
        $data = $this->getPageList($this->Model, $where);
        foreach ($data['list'] as &$value){
            $value['add_time'] = date('Y-m-d H:i',$value['add_time']);
            $value['user_name'] = Db::name('users')->where('user_id',$value['uid'])->value('nick_name');
        }
        $this->assign("data", $data);
        $this->assign("search", $search);
        if ($runData == false) {
            $data['content'] = $this->fetch('list')->getContent();
            unset($data['list']);
            return $this->success('', '', $data);
        }
        return true;
    }

}
